local skel = fk.CreateSkill {
  name = "emo__jiaocuo",
}

Fk:loadTranslationTable{
  ["emo__jiaocuo"] = "交错",
  [":emo__jiaocuo"] = "你的额定回合开始时，你可以跳过至多三个阶段，并摸等量张牌。"..
  "<br>其他角色的额定回合结束时，你可以执行一个其本回合未执行的阶段。",

  ["#emo__jiaocuo-choice"] = "交错：你可以跳过至多三个阶段，并摸等量张牌",
  ["#emo__jiaocuo-ask"] = "交错：你可以执行一个其本回合未执行的阶段",

  ["#GainAnPhaseTurn"] = "%from 执行一个仅有 %arg 的额外回合",
}

skel:addEffect(fk.TurnStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and not player:insideExtraTurn()
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local phases = table.map({Player.Start,Player.Judge, Player.Draw, Player.Play, Player.Discard, Player.Finish}, Util.PhaseStrMapper)
    local choices = room:askToChoices(player, {
      choices = phases, min_num = 1, max_num = 3, skill_name = skel.name, cancelable = true,
      prompt = "#emo__jiaocuo-choice",
    })
    if #choices > 0 then
      event:setCostData(self, table.map(choices, Util.PhaseStrMapper))
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local phases = event:getCostData(self)
    player:drawCards(#phases, skel.name)
    for _, phase in ipairs(phases) do
      player:skip(phase)
    end
  end,
})

skel:addEffect(fk.TurnEnd, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if not target:insideExtraTurn() and player:hasSkill(skel.name) and target ~= player then
      local phases = {2,3,4,5,6,7}
      player.room.logic:getEventsOfScope(GameEvent.Phase, 1, function(e)
        if e.data.who == target then
          table.removeOne(phases, e.data.phase)
        end
        return #phases == 0
      end, Player.HistoryTurn)
      if #phases > 0 then
        event:setCostData(self, table.map(phases, Util.PhaseStrMapper))
        return true
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local choices = room:askToChoices(player, {
      choices = event:getCostData(self), min_num = 1, max_num = 1, skill_name = skel.name, cancelable = true,
      prompt = "#emo__jiaocuo-ask",
    })
    if #choices > 0 then
      event:setCostData(self, Util.PhaseStrMapper(choices[1]))
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local phase = event:getCostData(self)
    player:gainAnExtraTurn(true, skel.name, {phase})
  end,
})


return skel
